package linkedListImp;

/**
 * @Author: Tiangle
 * @Date: 2021/9/29 18:02
 */
public class MyLinkdList<E> {
    private int index;
    private Node<E> startNode;

    public MyLinkdList(){
        index = 0;
        startNode = null;
    }

    public void add(E e){
        Node n = new Node(e);
        if (index==0){
            startNode = n;
            index++;
        }else{
            Node tempNode = startNode;
            for (int i = 1; i < index; i++) {
                tempNode = tempNode.getNext();
            }
            tempNode.setNext(n);
            index++;
        }
    }
    public boolean add(E e,int a){
        if (a>index||a<1)
            return false;
        Node n = new Node(e);

        if (a==0){
            Node tempNode = startNode;
            startNode = n;
            n.setNext(tempNode);
            index++;
        } else{
            Node tempNode = startNode;
            for (int i = 1; i < a; i++) {
                tempNode = tempNode.getNext();
            }
            Node nextNode = tempNode.getNext();

            tempNode.setNext(n);
            n.setNext(nextNode);
            index++;
        }

        return true;
    }
    public E get(int a){
        if (a>=index||a<0){
            return null;
        }
        Node<E> tempNode = startNode;
        for (int i = 0; i < a; i++) {
            tempNode = tempNode.getNext();
        }
        return tempNode.getData();
    }
    public void getAll(){
        Node<E> tempNode = startNode;
        for (int i = 0; i < index; i++) {
            if (i!=0)
                System.out.print(",");
            System.out.print(tempNode.getData());
            tempNode=tempNode.getNext();
        }
        System.out.println();
    }
    public boolean delete(int a){
        if (a>index||a<0)
            return false;

        if (a==0){
            startNode = startNode.getNext();
            index--;
        }else{
            Node tempNode = startNode;
            for (int i = 1; i < a; i++) {
                tempNode = tempNode.getNext();
            }
            tempNode.setNext(tempNode.getNext().getNext());
            index--;
        }

        return true;
    }

}
